﻿using System;
using System.IO;
using System.Net;
using System.Text;
/*
 * HTTP http =new HTTP();
 * http.Encode="UTF-8";
 * http.ProxyAdd="127.0.0.1:808";
 * String HTMLCode=http.Get("http://www.******.com");
 * 
 */
namespace Ue.Qcdz.Domain
{
    public class HttpRequest
    {

        public string Encode = "GB2312";//对方面编码，一般为GB2312和UTF-8两种
        public string ProxyAdd = "";

        #region ：A获取网页编码
        public string Get(string url)
        {
            string sContent = "";
            HttpStatusCode hc = new HttpStatusCode();

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse response;
            try
            {
                if (ProxyAdd != "")//设置HTTP代理
                {
                    WebProxy proxyObject = new WebProxy(ProxyAdd, true);
                    request.Proxy = proxyObject;
                }
                request.Method = "GET";
                request.Referer = url;
                request.ContentType = "text/html; charset=" + Encode;
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
                request.Accept = "*/*";

                //这里可以设置要发送过去的COOKIE内容
                //CookieContainer cookie = new CookieContainer();
                //Cookie ck = new Cookie("Test", "Test Content");
                //ck.Domain = "www.baidued.cn";
                //cookie.Add(ck);
                //request.CookieContainer = cookie;

                request.Timeout = 30000;
                response = (HttpWebResponse)request.GetResponse();
                hc = response.StatusCode;
                //if (response.ContentLength < 1)//如果对方返回的HTTP数据包没有Content-Length字段话,就要使用这种方式.
                //{
                //    int nreadsize;
                //    byte[] nbytes = new byte[512];//接收缓冲区
                //    Stream ns = null;
                //    FileStream fs = new FileStream(Directory.GetCurrentDirectory() + "/HTTP.txt", FileMode.Create);
                //    //接收的起始位置及接收的长度 
                //    request.AddRange(0);
                //    ns = response.GetResponseStream();//获得接收流
                //    nreadsize = ns.Read(nbytes, 0, 512);
                //    while (nreadsize > 0)
                //    {
                //        fs.Write(nbytes, 0, nreadsize);
                //        try
                //        {
                //            nreadsize = ns.Read(nbytes, 0, 512);//会出错,还没明白原因,正在想办法
                //        }
                //        catch { break; }
                //    }
                //    fs.Flush();
                //    fs.Close();
                //    fs.Dispose();
                //    ns.Close();
                //    ns.Dispose();
                //    //Duxer dux = new Duxer();
                //    //sContent = dux.ReadTxtFile(Directory.GetCurrentDirectory() + "/HTTP.txt", Encode);
                //    //dux.DeleteFile(Directory.GetCurrentDirectory() + "/HTTP.txt");
                //}
                //else
                //{
                    StreamReader stream = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(Encode));
                    sContent = stream.ReadToEnd();
                    stream.Close();
                //}
                response.Close();

                if (hc == HttpStatusCode.OK)//成功
                {
                    return sContent;
                }
                else
                {
                    return "Error:" + hc.ToString();
                }
            }
            catch (Exception ex)
            {
                return "Error:" + ",错误描述:" + ex.ToString() + ",远程地址:" + url;
            }
        }
        #endregion

        #region ：获取编码
        /// <summary>
        /// 获取网页源代码
        /// </summary>
        /// <param name="url">URL路径</param>
        /// <returns></returns>
        public string GetHTML(string url)
        {
            string html = string.Empty;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream stream = response.GetResponseStream();
                StreamReader reader = new StreamReader(stream, Encoding.GetEncoding(this.Encode));
                html = reader.ReadToEnd();
                stream.Close();
            }
            catch (Exception ex)
            {
                return "Error:" + ",错误描述:" + ex.ToString() + ",远程地址:" + url;
            }
            return html;

        }
        #endregion
    }
}
